/**
 * Created by user on 15-6-1.
 */
class GameController extends egret.DisplayObjectContainer{
    public constructor(){
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    private _house:egret.Bitmap;
    private _appleCon:egret.Sprite;
    private onAddToStage():void{
        this.x=egret.MainContext.instance.stage.stageWidth/2;
        this.y=250;
        this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        this._house=new egret.Bitmap();
        this._house.texture=RES.getRes("loading_logo2_png");
        this._house.anchorX= this._house.anchorY=0.5;
        this.addChild(this._house);
        this._appleCon=new egret.Sprite();
        this.addChild(this._appleCon);
    }
    public gameStart():void{
        this.visible=true;
        this._house.touchEnabled=true;
        this._house.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchHouse,this);
    }
    public gameOver():void{
        this._house.touchEnabled=false;
        this._house.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouchHouse,this);
    }
    public clear():void{
        while(this._appleCon.numChildren){
            this._appleCon.removeChildAt(0);
        }
    }
    private onTouchHouse(e:egret.TouchEvent):void{
        var stageW:number=egret.MainContext.instance.stage.stageWidth;
        var texture:egret.Texture = RES.getRes("apple_png");
        var rx:number=Math.floor(Math.random()*(stageW-texture.textureWidth))-stageW/2+texture.textureWidth/2;
        var ry:number=Math.floor(Math.random()*500)+this._house.height;
        var role:Role=new Role(1,(rx<=0?false:true),new egret.Point(0,0),new egret.Point(rx/2,-500),new egret.Point(rx,ry));
        this._appleCon.addChild(role);
        role.run();
        this.dispatchEvent(new GuiEvent(GuiEvent.CLICK_HOUSE));
    }

}
